package com.zzf.concurrent.example.design.pattern.two.phase.termination;

import lombok.extern.slf4j.Slf4j;

/**
 * @author GaoFeng2017
 * @date 2019/9/9 20:36
 */
@Slf4j
public class CounterThread extends Thread {

    private volatile boolean isTerminated;

    private int count;

    @Override
    public void run() {
        try {

            while (!isTerminated) {
                log.info("Thread is working, current count is {}", count++);
                Thread.sleep((long) (Math.random() * 2000));
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            this.clean();
        }
    }

    private void clean() {
        log.info("do some resource released, current count is {}", count);
    }

    public void shutdown() {
        this.isTerminated = true;
        this.interrupt();
    }
}
